/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_ssap_client_callback_proxy.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
int32_t SleSsapClientCallbackProxy::InnerTransact(
    uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
{
    if(code != SLE_SSAP_CLIENT_CALLBACK_WRITE_REASULT && code != SLE_SSAP_CLIENT_CALLBACK_NOTIFICATION
            && code != SLE_SSAP_CLIENT_CALLBACK_INDICATION) {
        SLE_LOGD("InnerTransact In");
    }
    auto remote = Remote();
    if (remote == nullptr) {
        SLE_LOGE("get remote failed, error code %{public}d", code);
        return OBJECT_NULL;
    }
    int32_t err = remote->SendRequest(code, data, reply, flags);
    if (err != NO_ERROR) {
        SLE_LOGE("send request failed, ipcErr=%{public}d code %{public}d", err, code);
    }
    return err;
}

void SleSsapClientCallbackProxy::OnFindService(
    uint8_t clientId, uint16_t connectId, const SsapClientFindServiceRes &result, uint32_t errCode)
{
    SLE_LOGD("OnFindService In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleSsapClientCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(clientId), "write client id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteParcelable(&result), "write find structure result failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t ret = InnerTransact(SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_FIND_SERVICE, option, data, reply);
    if (ret != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", ret);
        return;
    }
}

void SleSsapClientCallbackProxy::OnFindProperty(
    uint8_t clientId, uint16_t connectId, const SsapClientFindPropertyRes &result, uint32_t errCode)
{
    SLE_LOGD("OnFindProperty In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleSsapClientCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(clientId), "write client id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteParcelable(&result), "write find property result failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t ret = InnerTransact(SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_FIND_PROPERTY, option, data, reply);
    if (ret != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", ret);
        return;
    }
}

void SleSsapClientCallbackProxy::OnFindComplete(
    uint8_t clientId, uint16_t connectId, uint8_t type, const Uuid &uuid, uint32_t errCode)
{
    SLE_LOGD("OnFindComplete In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleSsapClientCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(clientId), "write client id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(type), "write type failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteParcelable(&uuid), "write uuid failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_FIND_COMPLETE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}

void SleSsapClientCallbackProxy::OnWriteResult(
    uint8_t clientId, uint16_t connectId, uint16_t handle, uint8_t type, uint32_t errCode)
{
#if 0
    SLE_LOGD("OnWriteResult In");
#endif
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleSsapClientCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(clientId), "write client id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(handle), "write write handle failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(type), "write write type failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_WRITE_REASULT, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}

void SleSsapClientCallbackProxy::OnReadResult(
    uint8_t clientId, uint16_t connectId, const SsapClientHandleValue &value, uint32_t errCode)
{
    SLE_LOGD("OnReadResult In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleSsapClientCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(clientId), "write client id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteParcelable(&value), "write read result value failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_READ_RESULT, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}

void SleSsapClientCallbackProxy::OnReadByUuidComplete(
    uint8_t clientId, uint16_t connectId, uint8_t type, const Uuid &uuid, uint32_t errCode)
{
    SLE_LOGD("OnReadByUuidComplete In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleSsapClientCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(clientId), "write client id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(type), "write type failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteParcelable(&uuid), "write uuid failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_READ_BY_UUID_COMPLETE,
        option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}

void SleSsapClientCallbackProxy::OnExchangeInfo(
    uint8_t clientId, uint16_t connectId, uint32_t mtu, uint16_t version, uint32_t errCode)
{
    SLE_LOGD("OnExchangeInfo In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleSsapClientCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(clientId), "write client id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(mtu), "write mtu failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(version), "write version failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_EXCHANGE_INFO, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}

void SleSsapClientCallbackProxy::OnNotification(
    uint8_t clientId, uint16_t connectId, const SsapClientHandleValue &value, uint32_t errCode)
{
#if 0
    SLE_LOGD("OnNotification In");
#endif
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleSsapClientCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(clientId), "write client id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteParcelable(&value), "write notification value failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_NOTIFICATION, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}

void SleSsapClientCallbackProxy::OnIndication(
    uint8_t clientId, uint16_t connectId, const SsapClientHandleValue &value, uint32_t errCode)
{
#if 0
    SLE_LOGD("OnIndication In");
#endif
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_LOG(data.WriteInterfaceToken(SleSsapClientCallbackProxy::GetDescriptor()),
        "write interface token failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint8(clientId), "write client id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint16(connectId), "write connnect id failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteParcelable(&value), "write indication value failed!");
    SLE_ASSERT_AND_RETURN_LOG(data.WriteUint32(errCode), "write result status failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_ASYNC};
    int32_t result = InnerTransact(SleSsapClientCallbackInterfaceCode::SLE_SSAP_CLIENT_CALLBACK_INDICATION, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return;
    }
}
} // SleStandard
} // NearLink
} // OHOS